import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useUserStore } from './userStore'
import { getNewCartAPI, insertCartAPI, delCartAPI, mergeCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart',() => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    // 抽象查询cart函数
    const updateCart = async () => {
        const res = await getNewCartAPI()
        cartList.value = res.result
    }
    // 定义state
    const cartList = ref([])
    // 定义action
    // 新增购物车
    const addCart = async (goods) => {
        const { skuId, count } = goods
        if (isLogin.value) {
            await insertCartAPI({skuId, count})
            updateCart()
        } else {
            // 添加购物车操作
            // 添加过，count + 1
            // 没添加过，直接push
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                // 找到了 count + 1
                item.count = item.count + goods.count
            } else {
                // 没找到
                cartList.value.push(goods)
            }
        }
    }
    // 删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            await delCartAPI([skuId])
            updateCart()
        } else {
            const idx = cartList.value.findIndex((item) => skuId === item.skuId)
            cartList.value.splice(idx, 1)
        }
    }
    // 退出清除购物车
    const clearCart = () => {
        cartList.value = []
    }
    // 合并购物车操作
    const mergeCart = async () => {
        await mergeCartAPI(cartList.value.map(item => {
            return {
                skuId: item.skuId,
                selected: item.selected,
                count: item.count
            }
        }))
        updateCart()
    }
    // 计算数量
    const allCount = computed(() => cartList.value.reduce((count, goods) => count + goods.count, 0 ))
    // 计算选中数量
    const selectedCount = computed(() => cartList.value.filter((item) => item.selected).reduce((count, goods) => count + goods.count, 0))
    // 计算价格
    const allPrice = computed(() => cartList.value.reduce((price, goods) => price + goods.count * goods.price, 0 ))
    // 计算选中价格
    const selectedPrice = computed(() => cartList.value.filter((item) => item.selected).reduce((price, goods) => price + goods.count * goods.price, 0))
    // 单选
    const singleCheck = (skuId, selected) => {
        const item = cartList.value.find((item) => item.skuId === skuId)
        item.selected = selected
    }
    // 全选
    const isAll = computed(() => cartList.value.every((item) => item.selected))
    const allCheck = (selected) => {
        cartList.value.forEach((item) => item.selected = selected)
    }

    return {
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        singleCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedPrice,
        clearCart,
        mergeCart,
        updateCart
    }
},{
    persist: true
})